{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "import time\n",
    "import xlsxwriter\n",
    "\n",
    "class Xlloginfo(object):\n",
    "    def __init__(self, path = ''):\n",
    "        fname = path + time.strftime('%Y-%m-%d', time.gmtime())\n",
    "        self.row = 0\n",
    "        self.xl = xlsxwriter.Workbook(path + fname + '.xlsx')\n",
    "        \n",
    "    def xlWrite(self, *args):\n",
    "        col = 0\n",
    "        for val in args:\n",
    "            self.sheet.write_string(self.row, col, val)\n",
    "            col += 1\n",
    "        self.row += 1\n",
    "    \n",
    "    def logInit(self, sheetname, *title):\n",
    "        self.sheet = self.xl.add_worksheet(sheetname)\n",
    "        self.sheet.set_column('A:E', 30)\n",
    "        self.xlWrite(*title)\n",
    "    \n",
    "    def logWrite(self, *args):\n",
    "        self.xlWrite(*args)\n",
    "        \n",
    "    def logClose(self):\n",
    "        self.xl.close()\n",
    "\n",
    "writeData = {'A1': '转盘页面是否关闭', 'A2': '已关闭'}\n",
    "\n",
    "webUrl = 'https://baohe-suduku.bestpeng.com/#/sudo?id=17&src=16&activityId=44'\n",
    "pageConfig = {}\n",
    "drawConfig = {'startBtn': 'start', 'prizeUseBtn': 'begin-envelope-get'}\n",
    "log = Xlloginfo()\n",
    "log.logInit('test', '测试标题', '结果')\n",
    "\n",
    "def dirver():\n",
    "    browser = webdriver.Chrome()\n",
    "    return browser\n",
    "\n",
    "def openUrl(dirver, url):\n",
    "    dirver.set_window_size(414, 800)\n",
    "    dirver.get(url)\n",
    "\n",
    "def draw(dirver, drawConfig, log):\n",
    "    \n",
    "    title = dirver.title # 获取title\n",
    "    dirver.find_element_by_class_name(drawConfig['startBtn']).click() # 点击抽奖按钮\n",
    "    \n",
    "    locator = (By.CLASS_NAME, drawConfig['prizeUseBtn']) # 开始抽奖\n",
    "    \n",
    "    try:\n",
    "        WebDriverWait(dirver, 5, 0.5).until(EC.presence_of_element_located(locator)) # 显示等待弹出框\n",
    "        drawUseButtonEvent = dirver.find_element_by_class_name(drawConfig['prizeUseBtn']).click() # 弹出框后点击使用奖品按钮\n",
    "        \n",
    "    except Exception as err:\n",
    "        print(err)\n",
    "        \n",
    "    finally:\n",
    "        time.sleep(1)\n",
    "        if title == dirver.title: # 如果title相同，则视为报错，没有访问成功\n",
    "            log.logWrite('是否成功抽奖并使用奖品', '失败')\n",
    "            dirver.get_screenshot_as_file(\"./img/test.png\") # 异常的时候，截图保存到本地\n",
    "        else:\n",
    "            log.logWrite('是否成功抽奖并使用奖品', '成功')\n",
    "            \n",
    "    dirver.back() # 返回到抽奖页\n",
    "    \n",
    "def dirverClose(dirver, log):\n",
    "    log.logClose()\n",
    "    dirver.close()\n",
    "    \n",
    "dirver = dirver()\n",
    "openUrl(dirver, webUrl)\n",
    "draw(dirver, drawConfig, log)\n",
    "time.sleep(2)\n",
    "dirverClose(dirver, log)\n",
    "\n",
    "logBook = xlsxwriter.Workbook('test.xlsx')\n",
    "logSheet = logBook.add_worksheet('test')\n",
    "logSheet.write('A1', '测试完毕')\n",
    "logBook.close()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
